package com.cloud.client.controller;

import com.cloud.client.service.AuthService;
import com.cloud.client.util.AjaxResult;
import com.cloud.client.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @packageName: com.cloud.client.controller
 * @name: AuthController
 * @description:
 * @author: 董定卓
 * @dateTime: 2019/7/12 16:47
 */
@RestController
public class AuthController {

    @Autowired
    AuthService service;

    /**
     * 用户登录
     *
     * @param map
     * @return
     */
    @PostMapping("/login")
    public Object login(@RequestParam Map<String, Object> map) {
        String account = MapUtil.getString(map, "account");
        if (account == null) {
            return AjaxResult.failResult("账号不能为空");
        }
        String password = MapUtil.getString(map, "password");
        if (password == null) {
            return AjaxResult.failResult("密码不能为空");
        }
        return service.login(map);
    }

    /**
     * 用户退出
     *
     * @param map
     * @return
     */
    @PostMapping("/logout")
    public Object logout(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        return service.logout(map);
    }

    /**
     * 获取权限列表
     *
     * @param map
     * @return
     */
    @PostMapping("/permissionPage")
    public Object permissionPage(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        return service.permissionPage(map);
    }

    /**
     * 添加角色
     *
     * @param map
     * @return
     */
    @PostMapping("/roleAdd")
    public Object roleAdd(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }

        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return AjaxResult.failResult("角色名称不能为空");
        }
        return service.roleAdd(map);
    }

    /**
     * 删除角色
     *
     * @param map
     * @return
     */
    @PostMapping("/roleDel")
    public Object roleDel(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String id = MapUtil.getString(map, "id");
        if (id == null) {
            return AjaxResult.failResult("角色ID不能为空");
        }
        return service.roleDel(map);
    }

    /**
     * 更新角色信息
     *
     * @param map
     * @return
     */
    @PostMapping("/roleUpdate")
    public Object roleUpdate(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String id = MapUtil.getString(map, "id");
        if (id == null) {
            return AjaxResult.failResult("角色ID不能为空");
        }
        String name = MapUtil.getString(map, "name");
        if (name == null && MapUtil.getString(map, "state") == null) {
            return AjaxResult.failResult("角色修改的内容不能为空");
        }
        return service.roleUpdate(map);
    }

    /**
     * 获取角色分页列表
     *
     * @param map
     * @return
     */
    @PostMapping("/rolePage")
    public Object rolePage(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        return service.rolePage(map);
    }

    /**
     * 添加用户
     *
     * @param map
     * @return
     */
    @PostMapping("/userAdd")
    public Object userAdd(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return AjaxResult.failResult("用户姓名不能为空");
        }
        String account = MapUtil.getString(map, "account");
        if (account == null) {
            return AjaxResult.failResult("用户账号不能为空");
        }
        String password = MapUtil.getString(map, "password");
        if (password == null) {
            return AjaxResult.failResult("用户密码不能为空");
        }
        return service.userAdd(map);
    }

    /**
     * 删除用户
     *
     * @param map
     * @return
     */
    @PostMapping("/userDel")
    public Object userDel(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String userId = MapUtil.getString(map, "userId");
        if (userId == null) {
            return AjaxResult.failResult("用户ID不能为空");
        }
        return service.userDel(map);
    }

    /**
     * 修改用户密码
     *
     * @param map
     * @return
     */
    @PostMapping("/userUpdatePassword")
    public Object userUpdatePassword(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String userId = MapUtil.getString(map, "userId");
        if (userId == null) {
            return AjaxResult.failResult("用户ID不能为空");
        }
        String password = MapUtil.getString(map, "password");
        if (password == null) {
            return AjaxResult.failResult("密码不能为空");
        }
        return service.userUpdatePassword(map);
    }

    /**
     * 修改用户信息
     *
     * @param map
     * @return
     */
    @PostMapping("/userUpdateInfo")
    public Object userUpdateInfo(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String name = MapUtil.getString(map, "name");
        String state = MapUtil.getString(map, "state");
        if (name == null && state == null) {
            return AjaxResult.failResult("修改的内容不能为空");
        }
        return service.userUpdateInfo(map);
    }

    /**
     * 用户分页列表
     *
     * @param map
     * @return
     */
    @PostMapping("/userPage")
    public Object userPage(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        return service.userPage(map);
    }

    /**
     * 用户角色添加
     *
     * @param map
     * @return
     */
    @PostMapping("/userRoleAdd")
    public Object userRoleAdd(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String userId = MapUtil.getString(map, "userId");
        if (userId == null) {
            return AjaxResult.failResult("用户ID不能为空");
        }
        return service.userRoleAdd(map);
    }

    /**
     * 用户角色列表
     *
     * @param map
     * @return
     */
    @PostMapping("/userRoleList")
    public Object userRoleList(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String userId = MapUtil.getString(map, "userId");
        if (userId == null) {
            return AjaxResult.failResult("用户ID不能为空");
        }
        return service.userRoleList(map);
    }


    /**
     * 角色分配权限
     *
     * @param map
     * @return
     */
    @PostMapping("/rolePermissionAdd")
    public Object rolePermissionAdd(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String roleId = MapUtil.getString(map, "roleId");
        if (roleId == null) {
            return AjaxResult.failResult("角色ID不能为空");
        }
        return service.rolePermissionAdd(map);
    }

    /**
     * 角色权限列表
     *
     * @param map
     * @return
     */
    @PostMapping("/rolePermissionList")
    public Object rolePermissionList(@RequestParam Map<String, Object> map) {
        String token = MapUtil.getString(map, "token");
        if (token == null) {
            return AjaxResult.failResult("token不能为空");
        }
        String roleId = MapUtil.getString(map, "roleId");
        if (roleId == null) {
            return AjaxResult.failResult("角色ID不能为空");
        }
        return service.rolePermissionList(map);
    }


}
